package com.datas.controller.baseentity;

import com.datas.beans.ActResult;
import com.datas.beans.DatatablesPagerWithRootFilter;
import com.datas.beans.DeptListTreeParam;
import com.datas.db.hibernatebeans.UmsDepartment;
import com.datas.service.baseentity.DepartmentsService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by jige0727 on 2017/6/12.
 * 部门
 */
@Controller
@RequestMapping(value = "/department", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public class DepartmentController {
    @Autowired
    DepartmentsService departmentsService;

    @ResponseBody
    @RequestMapping(value = "/create")
    public String create(@RequestBody UmsDepartment dataIn) {
        Integer res = departmentsService.create(dataIn);
        return new ActResult(res, "操作完成", "错误").toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/modify")
    public String modify(@RequestBody UmsDepartment dataIn) {
        Integer res = departmentsService.modify(dataIn);
        return new ActResult(res, "操作完成", "错误").toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/delete")
    public String delete(@RequestBody Integer[] ids) {
        Integer errCount = 0;
        Integer success = 1;
        for (Integer id : ids) {
            Integer res = departmentsService.delete(id);
            if (res == null || res == 0) {
                ++errCount;
                success = 0;
            }
        }
        return new ActResult(success, "操作完成", errCount + "个错误").toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/info")
    public String list(Integer id, HttpServletResponse response) {
        UmsDepartment res = departmentsService.info(id);
        if (res != null) {
            return new JSONObject(res).toString();
        }
        response.setStatus(500);
        return new ActResult(0, "错误").toString();
    }

    @ResponseBody
    @RequestMapping(value = "/list")
    public String list(@RequestBody DatatablesPagerWithRootFilter pager) {
        departmentsService.list(pager);
//        LoggerFactory.getLogger(getClass()).info("pager.getFinalData() -> " + pager.getFinalData());
        return pager.getFinalData().toString();
    }

    //////////////////

    /**
     * 界面上面的树形结构
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list-tree")
    public String listTree(DeptListTreeParam param) {
        JSONArray data = departmentsService.listTree(param);
        return data.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/list-tree/getselected")
    public String listTreeSelected(Integer userid) {
        JSONArray data = departmentsService.listTreeWithRoleGL(userid);
        return data.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/list-tree/getMngselected")
    public String listTreeMngSelected(Integer userid) {
        JSONArray data = departmentsService.listTreeForMngDept(userid);
        return data.toString();
    }
}
